package com.hatchsolution.ftp.pool.impl;



import java.io.InputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.pool2.impl.GenericObjectPool;

import com.hatchsolution.ftp.PathExtend;
import com.hatchsolution.ftp.exception.FTPConfigException;
import com.hatchsolution.ftp.factory.FTPClientFactory;
import com.hatchsolution.ftp.pool.FTPPoolConfig;
import com.hatchsolution.ftp.pool.FTPPoolService;



public class FTPPool implements FTPPoolService {
	private GenericObjectPool<FTPClient> pool;
	
	private FTPPoolConfig config ;
	
	private FTPClientFactory factory ;

	
	
	public FTPPool(FTPClientFactory factory) throws FTPConfigException {
		this.factory = factory;
		this.config = this.factory.getConfig();
		if(this.factory != null){
			this.factory.setConfig(config);
		}else{
			throw new FTPConfigException("Config is null, We must refer config class") ;
		}
		this.pool = new GenericObjectPool<FTPClient>(this.factory,this.config)  ;
	}


	public FTPPool(FTPPoolConfig config, FTPClientFactory factory) throws FTPConfigException {
		this.config = config;
		this.factory = factory;
		if(this.factory != null){
			this.factory.setConfig(config);
		}else{
			throw new FTPConfigException("Config is null, We must refer config class") ;
		}
		this.pool = new GenericObjectPool<FTPClient>(this.factory,this.config)  ;
		
	}


	@Override
	public FTPClient borrowObject() throws Exception {
		if(this.pool != null){
			return this.pool.borrowObject() ;
		}
		return null;
	}

	@Override
	public void returnObject(FTPClient ftpClient) {
		if(this.pool != null && ftpClient != null){
			this.pool.returnObject(ftpClient);
		}
		
	}
	
	
	public GenericObjectPool<FTPClient> getPool() {
		return pool;
	}

	public void setPool(GenericObjectPool<FTPClient> pool) {
		this.pool = pool;
	}

	public FTPPoolConfig getConfig() {
		return config;
	}

	public void setConfig(FTPPoolConfig config) {
		this.config = config;
	}

	public FTPClientFactory getFactory() {
		return factory;
	}

	public void setFactory(FTPClientFactory factory) {
		this.factory = factory;
	}
	
	public static void main(String[] args) throws Exception {
		FTPPoolConfig config = new FTPPoolConfig() ;
		config.setHost("192.168.1.20");
		config.setPort(21);
		config.setUsername("elen");
		config.setPassword("Elen@123");
		config.setPassiveMode("true");
		config.setClientTimeout(300000);
		config.setTransferFileType(2);
		//config.setMaxTotal(10);
		config.setMaxIdle(5);
		config.setMinIdle(1);
		config.setTestOnBorrow(true);
		config.setTestOnReturn(true);
		config.setTestWhileIdle(true);
		config.setNumTestsPerEvictionRun(10);
		config.setTimeBetweenEvictionRunsMillis(60000);
		
		FTPClientFactory f = new FTPClientFactory() ;
		f.setConfig(config);
	    final FTPPool pool = new FTPPool(config, f) ;
		FTPClient ftpClient = pool.borrowObject() ;
		ftpClient.changeWorkingDirectory(PathExtend.getDirectory("/uploadLogo/0d26051c-c13e-4f38-b40f-6bf8ba84896fcorporatioId22.bmp") );
		String ftpFileName = PathExtend.getFileName("/uploadLogo/0d26051c-c13e-4f38-b40qf-6bf8ba84896fcorporatioId22.bmp") ;
		InputStream inputStream = ftpClient.retrieveFileStream(ftpFileName);
		int returnCode = ftpClient.getReplyCode();
		if (inputStream == null || returnCode == 550) {
		   System.out.println("false");
		}else{
			System.out.println("true"); 
		}
	    

		
	}




	
	

}
